<template>
  <div>
    <PageDialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <!-- 条件栏 -->
      <NewPageFilter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
        :filter-btn="filterInfo.btnList"
      >
      </NewPageFilter>

      <el-form :model="tableInfo" ref="tableForm" style="width: 100%">
        <DialogTable
          ref="productTable"
          :refresh="tableInfo.refresh"
          :init-curpage="tableInfo.initCurpage"
          v-model:data="tableInfo.data"
          :api="tableInfo.api"
          :listen-height="false"
          :reserveSelection="true"
          v-model:query="filterInfo.query"
          :field-list="tableInfo.fieldList"
          :handle="tableInfo.handle"
          :highlight-row="highlightRow"
          :check-box="checkBox"
          @handleEvent="handleEvent"
        >
        </DialogTable>
      </el-form>
    </PageDialog>
  </div>
</template>

<script setup name="selectPendReceiptOrder">
import { receiptOrderDetail } from "@/api/receipt";

const { proxy } = getCurrentInstance();
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {};
    },
  },
  api: {
    type: Function,
    default: receiptOrderDetail,
  },
  selectData: {
    type: Array,
  },
});
// 过滤相关配置
const filterInfo = ref({
  query: {
    ...props.query,
    pageSize: 999,
    // sort: 'order_no desc',
  },
  list: [
    {
      componentType: "input",
      label: "发票号",
      value: "receiptNumber",
      event: search,
    },
    {
      componentType: "datetimerange",
      label: "创建时间",
      value: "createTime",
      event: search,
    },
    {
      componentType: "datetimerange",
      label: "审核时间",
      value: "updateTime",
      event: search,
    },
  ],
  btnList: [{ type: "button", label: "搜索", event: search }],
});
// 表格相关
const tableInfo = ref({
  api: null,
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    {
      label: "发票号",
      value: "receiptNumber",
    },
    {
      label: "发票金额",
      value: "shouldPayAmount",
      formatter: proxy.$fn.TripartiteMethod,
    },
    {
      label: "创建时间",
      value: "createTime",
    },
    {
      label: "审核时间",
      value: "updateTime",
    },
  ],
});
// 弹窗相关
const dialogInfo = ref({
  title: "可选发票",
  btLoading: false,
  width: "90%",
  btList: [
    { label: "关闭", event: handleClose, show: true },
    {
      label: "保存",
      type: "primary",
      disabled: true,
      event: save,
      show: true,
    },
  ],
});
const selectRow = ref("");
const shows = computed({
  get() {
    return props.showDialog;
  },
  set(val) {
    proxy.$emit("update:showDialog", val);
  },
});

// 获取列表
function getList() {
  tableInfo.value.api = receiptOrderDetail;
  tableInfo.value.refresh = Math.random();
}
function search() {
  if (filterInfo.value.query.createTime) {
    filterInfo.value.query.createTimeGreater =
      filterInfo.value.query.createTime.split(",")[0];
    filterInfo.value.query.createTimeLess =
      filterInfo.value.query.createTime.split(",")[1];
  } else {
    filterInfo.value.query.createTimeGreater = "";
    filterInfo.value.query.createTimeLess = "";
  }
  if (filterInfo.value.query.updateTime) {
    filterInfo.value.query.updateTimeGreater =
      filterInfo.value.query.updateTime.split(",")[0];
    filterInfo.value.query.updateTimeLess =
      filterInfo.value.query.updateTime.split(",")[1];
  } else {
    filterInfo.value.query.updateTimeGreater = "";
    filterInfo.value.query.updateTimeLess = "";
  }
  filterInfo.value.query.createTime = "";
  filterInfo.value.query.updateTime = "";
  getList();
}

function save() {
  if (selectRow.value && selectRow.value.length) {
    // 优惠金额默认为0
    for (const key in selectRow.value) {
      if (Object.hasOwnProperty.call(selectRow.value, key)) {
        selectRow.value[key].discountsAmount = selectRow.value[key].discountsAmount || 0
        selectRow.value[key].payAmount = selectRow.value[key].shouldPayAmount - selectRow.value[key].discountsAmount || 0
      }
    }
    let extDetails = selectRow.value
    handleClose();
    proxy.$emit("callback", extDetails);
  } else {
    proxy.$message({
      type: "warning",
      duration: 3500,
      showClose: true,
      message: "请选择产品",
    });
  }
}
function handleEvent(event, data) {
  switch (event) {
    case "list":
      tableInfo.value.data.forEach((item) => {
        item["price"] = item.settlementPrice;
        item["calcQty"] = item.availableQty;
      });
      break;
    case "tableClickRow":
      selectRow.value = data;
      dialogInfo.value.btList[1].disabled = !selectRow.value;
      break;
    case "tableCheck":
      if (data.length) {
        selectRow.value = data;
        dialogInfo.value.btList[1].disabled = !selectRow.value;
      }
      break;
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false;
  proxy.$emit("reset");
}
onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    getList();
  });
});
</script>

<style scoped></style>
